package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.product.entity.Brand;
import com.pig4cloud.mall.product.service.BrandService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author fangyu
 * @date 2020-11-25 15:05:29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/brand" )
@Api(value = "brand", tags = "品牌管理")
public class BrandController {

    private final BrandService brandService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param brand 品牌
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('product_brand_get')" )
    public R getBrandPage(Page page, Brand brand) {
        return R.ok(brandService.page(page, Wrappers.query(brand)));
    }


	@ApiOperation(value = "所有品牌", notes = "所有品牌")
	@GetMapping("/list" )
	@PreAuthorize("@pms.hasPermission('product_brand_get')" )
    public R getBrandList() {
		return R.ok(brandService.list());
	}

    /**
     * 通过id查询品牌
     * @param brandId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{brandId}" )
    @PreAuthorize("@pms.hasPermission('product_brand_get')" )
    public R getById(@PathVariable("brandId" ) Long brandId) {
        return R.ok(brandService.getById(brandId));
    }

    /**
     * 新增品牌
     * @param brand 品牌
     * @return R
     */
    @ApiOperation(value = "新增品牌", notes = "新增品牌")
    @SysLog("新增品牌" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('product_brand_add')" )
    public R save(@Valid @RequestBody Brand brand, BindingResult result) {
    	if (result.hasErrors()) {
    		return R.failed(result.getAllErrors());
		}
        return R.ok(brandService.save(brand));
    }

    /**
     * 修改品牌
     * @param brand 品牌
     * @return R
     */
    @ApiOperation(value = "修改品牌", notes = "修改品牌")
    @SysLog("修改品牌" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('product_brand_edit')" )
    public R updateById(@RequestBody Brand brand) {
        return R.ok(brandService.updateById(brand));
    }

    /**
     * 通过id删除品牌
     * @param brandId id
     * @return R
     */
    @ApiOperation(value = "通过id删除品牌", notes = "通过id删除品牌")
    @SysLog("通过id删除品牌" )
    @DeleteMapping("/{brandId}" )
    @PreAuthorize("@pms.hasPermission('product_brand_del')" )
    public R removeById(@PathVariable Long brandId) {
        return R.ok(brandService.removeById(brandId));
    }

}
